//
//  UIBarButtonItem+CycleItem.m
//  CycleControlFunction
//
//  Created by ddy on 2018/8/7.
//  Copyright © 2018年 ddy. All rights reserved.
//

#import "UIBarButtonItem+CycleItem.h"
#import <objc/runtime.h>

@implementation UIBarButtonItem (CycleItem)

/**
 * 1.创建导航按钮(只含图片)
 */
+ (UIBarButtonItem *)cycle_itemWithTarget:(id)target action:(SEL)action frame:(CGRect)frame norImg:(NSString *)normalImg highImg:(NSString *)highImg imgEdgeInsets:(UIEdgeInsets)edgeInsets{
    //创建按钮
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    //设置frame
    button.frame = frame;
    //设置normal状态下的图片
    [button setImage:[UIImage imageNamed:normalImg] forState:UIControlStateNormal];
    //设置高亮状态下的图片
    [button setImage:[UIImage imageNamed:highImg] forState:UIControlStateHighlighted];
    //设置图片偏离程度
    button.imageEdgeInsets = edgeInsets;
    //点击
    [button addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    //return UIBarButtonItem
    return [[UIBarButtonItem alloc]initWithCustomView:button];
}
/**
 * 2.创建导航按钮(只有标题)
 */
+(UIBarButtonItem *)cycle_itenWithTarget:(id)target action:(SEL)action fSize:(CGFloat)fontSize WeightFont:(CGFloat)weight btnTAlignment:(NSTextAlignment)textAlignMent normalTitle:(NSString *)normalTitle normalColor:(UIColor *)normalColor frame:(CGRect)rectFrame TitleEdgeInsets:(UIEdgeInsets)edgeInsets{
    ///1.左边按钮
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    ///2.设置普通状态下按钮颜色
    [btn setTitleColor:normalColor forState:UIControlStateNormal];
    ///3.设置普通状态下 按钮文本
    [btn setTitle:normalTitle forState:UIControlStateNormal];
    ///4.设置按钮对齐方式
    btn.titleLabel.textAlignment = textAlignMent;
    ///5.设置字体和加粗程度
    if (@available(iOS 8.2, *)) {
        btn.titleLabel.font = [UIFont systemFontOfSize:fontSize weight:weight];
    } else {
        // Fallback on earlier versions
        btn.titleLabel.font = [UIFont systemFontOfSize:fontSize];
    }
    ///6.添加事件
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    ///7.设置frame
    btn.frame = rectFrame;
    ///8.设置标题偏离程度
    btn.titleEdgeInsets = edgeInsets;
    
    return  [[UIBarButtonItem alloc]initWithCustomView:btn];
}
/**
 * 3.创建按钮 (设置图片,sizeTofit方式)
 */
+ (UIBarButtonItem *)cycle_barButtonItemWithImage:(UIImage *)image highImage:(UIImage *)highImage target:(id)target action:(SEL)action
{
    // btn
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setBackgroundImage:image forState:UIControlStateNormal];
    [btn setBackgroundImage:highImage forState:UIControlStateHighlighted];
    [btn sizeToFit];
    
    [btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
    
    return  [[UIBarButtonItem alloc] initWithCustomView:btn];
}

@end
